package com.itboy.mall.pms.product.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itboy.mall.pms.attr.domain.MallAttr;
import com.itboy.mall.pms.attr.service.IMallAttrService;
import com.itboy.mall.sms.bounds.domain.MallSpuBounds;
import com.itboy.mall.sms.bounds.service.IMallSpuBoundsService;
import com.itboy.mall.sms.memberprice.domain.MallMemberPrice;
import com.itboy.mall.sms.memberprice.service.IMallMemberPriceService;
import com.itboy.mall.pms.product.domain.*;
import com.itboy.mall.pms.product.service.ProductService;
import com.itboy.mall.pms.productattrvalue.domain.MallProductAttrValue;
import com.itboy.mall.pms.productattrvalue.service.IMallProductAttrValueService;
import com.itboy.mall.sms.skufullreduction.domain.MallSkuFullReduction;
import com.itboy.mall.sms.skufullreduction.service.IMallSkuFullReductionService;
import com.itboy.mall.pms.skuimages.domain.MallSkuImages;
import com.itboy.mall.pms.skuimages.service.IMallSkuImagesService;
import com.itboy.mall.pms.skuinfo.domain.MallSkuInfo;
import com.itboy.mall.pms.skuinfo.service.IMallSkuInfoService;
import com.itboy.mall.sms.skuladder.domain.MallSkuLadder;
import com.itboy.mall.sms.skuladder.service.IMallSkuLadderService;
import com.itboy.mall.pms.skusaleattrvalue.domain.MallSkuSaleAttrValue;
import com.itboy.mall.pms.skusaleattrvalue.service.IMallSkuSaleAttrValueService;
import com.itboy.mall.pms.spuimages.domain.MallSpuImages;
import com.itboy.mall.pms.spuimages.service.IMallSpuImagesService;
import com.itboy.mall.pms.spuinfo.domain.MallSpuInfo;
import com.itboy.mall.pms.spuinfo.service.IMallSpuInfoService;
import com.itboy.mall.pms.spuinfodesc.domain.MallSpuInfoDesc;
import com.itboy.mall.pms.spuinfodesc.service.IMallSpuInfoDescService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ProductServiceImpl implements ProductService {

	/**
	 * sup service
	 */
	private final IMallSpuInfoService iMallSpuInfoService;

	/**
	 * sup 保存图集 service
	 */
	private final IMallSpuImagesService iMallSpuImagesService;

	/**
	 * sup 保存描述图集 service
	 */
	private final IMallSpuInfoDescService iMallSpuInfoDescService;

	/**
	 * spu 保存规格参数 service
	 */
	private final IMallProductAttrValueService iMallProductAttrValueService;

	/**
	 * spu 积分 service
	 */
	private final IMallSpuBoundsService iMallSpuBoundsService;

	/**
	 * sku service
	 */
	private final IMallSkuInfoService iMallSkuInfoService;

	/**
	 * sku 保存销售属性 service
	 */
	private final IMallSkuSaleAttrValueService iMallSkuSaleAttrValueService;

	/**
	 * sku 保存图集 service
	 */
	private final IMallSkuImagesService iMallSkuImagesService;

	/**
	 * sku 商品满减信息 service
	 */
	private final IMallSkuFullReductionService iMallSkuFullReductionService;


	/**
	 * sku 商品阶梯价格  满几件打几折 service
	 */
	private final IMallSkuLadderService iMallSkuLadderService;

	/**
	 * sku 商品会员价格 service
	 */
	private final IMallMemberPriceService iMallMemberPriceService;

	/**
	 * 属性service
	 */
	private final IMallAttrService iMallAttrService;



	@Transactional
	@Override
	public boolean save(SpuSaveInfo spuSaveInfo) {
		boolean flag=false;
		//1.保存spuInfo信息 mall_spu_info
		MallSpuInfo mallSpuInfo = new MallSpuInfo();
		BeanUtils.copyProperties(spuSaveInfo,mallSpuInfo);
		iMallSpuInfoService.save(mallSpuInfo);

		//2.保存spuInfo描述图片 mall_spu_info_desc
		List<String> decript = spuSaveInfo.getDecript();
		if(CollectionUtil.isNotEmpty(decript)){
			MallSpuInfoDesc mallSpuInfoDesc = new MallSpuInfoDesc();
			mallSpuInfoDesc.setSpuId(mallSpuInfo.getId());
			mallSpuInfoDesc.setDecript(String.join(",",decript));
			iMallSpuInfoDescService.save(mallSpuInfoDesc);
		}

		//3.保存spuInfo的图片集 mall_spu_images
		List<String> images = spuSaveInfo.getImages();
		if(CollectionUtil.isNotEmpty(images)){
			List<MallSpuImages> mallSpuImagesList = images.stream().map(image -> {
				MallSpuImages mallSpuImages = new MallSpuImages();
				mallSpuImages.setSpuId(mallSpuInfo.getId());
				mallSpuImages.setImgUrl(image);
				return mallSpuImages;
			}).filter(entity->{
				return StringUtils.isNotBlank(entity.getImgUrl());
			}).collect(Collectors.toList());
			iMallSpuImagesService.saveBatch(mallSpuImagesList);
		}
		//4.保存spuInfo的规格参数 mall_product_attr_value
		List<BaseAttrs> baseAttrs = spuSaveInfo.getBaseAttrs();
		if(CollectionUtil.isNotEmpty(baseAttrs)){
			List<MallProductAttrValue> productAttrValueList = baseAttrs.stream().map(attr -> {
				MallProductAttrValue mallProductAttrValue = new MallProductAttrValue();
				mallProductAttrValue.setSpuId(mallSpuInfo.getId());
				mallProductAttrValue.setAttrId(attr.getAttrId());
				if(attr.getAttrId()!=null){
					//查询属性
					MallAttr mallAttr = iMallAttrService.getById(attr.getAttrId());
					mallProductAttrValue.setAttrName(ObjectUtil.isNotEmpty(mallAttr)?mallAttr.getAttrName():"");
				}
				mallProductAttrValue.setAttrValue(attr.getAttrValues());
				mallProductAttrValue.setQuickShow(attr.getShowDesc());
				return mallProductAttrValue;
			}).collect(Collectors.toList());
			iMallProductAttrValueService.saveBatch(productAttrValueList);
		}


		//5.保存spuInfo积分信息 mall_spu_bounds
		Bounds bounds = spuSaveInfo.getBounds();
		if(ObjectUtil.isNotNull(bounds)){
			MallSpuBounds mallSpuBounds = new MallSpuBounds();
			BeanUtils.copyProperties(bounds,mallSpuBounds);
			mallSpuBounds.setSpuId(mallSpuInfo.getId());
			iMallSpuBoundsService.save(mallSpuBounds);
		}

		//6.1保存sku对应的sku信息 mall_sku_info
		List<SkuInfo> skuInfos = spuSaveInfo.getSkus();
		if(CollectionUtil.isNotEmpty(skuInfos)){
			skuInfos.forEach(skuItem->{
				MallSkuInfo mallSkuInfo = new MallSkuInfo();
				BeanUtils.copyProperties(skuItem,mallSkuInfo);
				mallSkuInfo.setSpuId(mallSpuInfo.getId());
				mallSkuInfo.setBrandId(mallSpuInfo.getBrandId());
				mallSkuInfo.setCatalogId(mallSpuInfo.getCatalogId());
				mallSkuInfo.setSaleCount(0L);
				String skuDefaultImg="";
				if(CollectionUtil.isNotEmpty(skuItem.getImages())){
					for (SkuImages skuImage : skuItem.getImages()) {
						if(skuImage.getDefaultImg()==1){
							skuDefaultImg=skuImage.getImgUrl();
						}

					}
				}

				mallSkuInfo.setSkuDefaultImg(skuDefaultImg);
				iMallSkuInfoService.save(mallSkuInfo);
		       //6.2保存sku图片信息 mall_sku_images
				List<SkuImages> skuImages = skuItem.getImages();
				if (CollectionUtil.isNotEmpty(skuImages)){
					List<MallSkuImages> skuImagesList = skuImages.stream().map(img -> {
						MallSkuImages mallSkuImages = new MallSkuImages();
						mallSkuImages.setSkuId(mallSkuInfo.getSkuId());
						mallSkuImages.setImgUrl(img.getImgUrl());
						mallSkuImages.setDefaultImg(img.getDefaultImg());
						return mallSkuImages;
					}).filter(entity->{
						return StringUtils.isNotBlank(entity.getImgUrl());
					}).collect(Collectors.toList());
					iMallSkuImagesService.saveBatch(skuImagesList);
				}
				//6.3保存sku对应的销售属性 mall_sku_sale_attr_value
				List<SaleAttr> saleAttrs = skuItem.getAttr();
				if (CollectionUtil.isNotEmpty(saleAttrs)){
					List<MallSkuSaleAttrValue> skuSaleAttrValueList = saleAttrs.stream().map(attr -> {
						MallSkuSaleAttrValue mallSkuSaleAttrValue = new MallSkuSaleAttrValue();
						mallSkuSaleAttrValue.setSkuId(mallSkuInfo.getSkuId());
						mallSkuSaleAttrValue.setAttrValue(attr.getAttrValue());
						mallSkuSaleAttrValue.setAttrId(attr.getAttrId());
						if (attr.getAttrId() != null) {
							//查询属性
							MallAttr mallAttr = iMallAttrService.getById(attr.getAttrId());
							mallSkuSaleAttrValue.setAttrName(ObjectUtil.isNotEmpty(mallAttr) ? mallAttr.getAttrName() : "");
						}
						return mallSkuSaleAttrValue;
					}).collect(Collectors.toList());
					iMallSkuSaleAttrValueService.saveBatch(skuSaleAttrValueList);
				}
				//6.4保存sku的优惠满减信息
				if(skuItem.getFullCount()>0||skuItem.getFullPrice().compareTo(new BigDecimal("0"))==1){
                   // 满几件打几折=>mall_sku_ladder
					MallSkuLadder mallSkuLadder = new MallSkuLadder();
					mallSkuLadder.setFullCount(skuItem.getFullCount());
					mallSkuLadder.setDiscount(skuItem.getDiscount());
					mallSkuLadder.setAddOther(skuItem.getCountStatus());
					mallSkuLadder.setSkuId(mallSkuInfo.getSkuId());
					if(mallSkuLadder.getFullCount()>0){
						iMallSkuLadderService.save(mallSkuLadder);
					}

					// 会员价格=》mall_member_price
					List<MemberPrice> memberPrice = skuItem.getMemberPrice();
					if(CollectionUtil.isNotEmpty(memberPrice)){
						List<MallMemberPrice> memberPriceList = memberPrice.stream().map(mPrice -> {
							MallMemberPrice mallMemberPrice = new MallMemberPrice();
							mallMemberPrice.setSkuId(mallSkuInfo.getSkuId());
							mallMemberPrice.setMemberLevelId(mPrice.getId());
							mallMemberPrice.setMemberLevelName(mPrice.getName());
							mallMemberPrice.setMemberPrice(mPrice.getPrice());
							mallMemberPrice.setAddOther(1);
							return mallMemberPrice;
						}).filter(entity->{
							return entity.getMemberPrice().compareTo(new BigDecimal("0"))==1;
						}).collect(Collectors.toList());
						iMallMemberPriceService.saveBatch(memberPriceList);
					}
					// 满减信息=》mall_sku_full_reduction
					MallSkuFullReduction mallSkuFullReduction = new MallSkuFullReduction();
					BeanUtils.copyProperties(skuItem,mallSkuFullReduction);
					mallSkuFullReduction.setSkuId(mallSkuInfo.getSkuId());
					if(mallSkuFullReduction.getFullPrice().compareTo(new BigDecimal("0"))==1){
						iMallSkuFullReductionService.save(mallSkuFullReduction);
					}

				}
			});
			flag=true;

		}

		return flag;
	}
}
